package com.song.threadpool;

/**
 * @Author Klys
 * @Date 2023/6/20 10:14
 * @Description
 * @Version 1.0
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolExample {
    public static void main(String[] args) {
        // Executors创建一个固定大小的线程池，大小为 5
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        // 提交 10 个任务给线程池
        for (int i = 0; i < 10; i++) {
            executorService.execute(new Task(i));
        }
        // 关闭线程池
        executorService.shutdown();
    }

    static class Task implements Runnable {
        private int taskId;

        public Task(int taskId) {
            this.taskId = taskId;
        }

        @Override
        public void run() {
            System.out.println("Task " + taskId + " is running on thread " + Thread.currentThread().getName());
        }
    }
}
